// MMORPG游戏状态管理器
import { GAME_STATES } from './types';
import type { TransitionMap, StateActionMap, GameState } from './types';

/**
 * 状态管理器类，负责管理游戏状态转换条件和状态行为
 */
export class StateManager {
  // 状态转换条件映射
  private transitions: TransitionMap;

  // 状态行为映射
  private stateActions: StateActionMap;

  constructor() {
    this.transitions = {};
    this.stateActions = {};
    this.initTransitions();
    this.initStateActions();
  }

  /**
   * 初始化状态转换条件
   */
  private initTransitions(): void {
    this.transitions = {
      [GAME_STATES.EXPLORATION]: [
        { to: GAME_STATES.COMBAT, condition: () => false }, // 实际游戏中应检查玩家是否遭遇敌人
        { to: GAME_STATES.DIALOGUE, condition: () => false }, // 实际游戏中应检查玩家是否接近可对话的NPC
        { to: GAME_STATES.TRADING, condition: () => false }, // 实际游戏中应检查玩家是否接近可交易的NPC或玩家
        { to: GAME_STATES.MENU, condition: () => false }, // 实际游戏中应检查玩家是否按下菜单键
        { to: GAME_STATES.CHARACTER_PANEL, condition: () => false }, // 实际游戏中应检查玩家是否按下角色面板键
        { to: GAME_STATES.INVENTORY, condition: () => false }, // 实际游戏中应检查玩家是否按下物品栏键
        { to: GAME_STATES.DEATH, condition: () => false } // 实际游戏中应检查玩家生命值是否为0
      ],
      [GAME_STATES.COMBAT]: [
        { to: GAME_STATES.EXPLORATION, condition: () => false }, // 实际游戏中应检查所有敌人是否已被击败
        { to: GAME_STATES.DEATH, condition: () => false }, // 实际游戏中应检查玩家生命值是否为0
        { to: GAME_STATES.MENU, condition: () => false } // 实际游戏中应检查玩家是否按下菜单键
      ],
      [GAME_STATES.DIALOGUE]: [
        { to: GAME_STATES.EXPLORATION, condition: () => false }, // 实际游戏中应检查对话是否已结束
        { to: GAME_STATES.MENU, condition: () => false } // 实际游戏中应检查玩家是否按下菜单键
      ],
      [GAME_STATES.TRADING]: [
        { to: GAME_STATES.EXPLORATION, condition: () => false }, // 实际游戏中应检查交易是否已完成或取消
        { to: GAME_STATES.MENU, condition: () => false } // 实际游戏中应检查玩家是否按下菜单键
      ],
      [GAME_STATES.MENU]: [
        { to: GAME_STATES.EXPLORATION, condition: () => false } // 实际游戏中应检查菜单是否已关闭
      ],
      [GAME_STATES.CHARACTER_PANEL]: [
        { to: GAME_STATES.EXPLORATION, condition: () => false } // 实际游戏中应检查角色面板是否已关闭
      ],
      [GAME_STATES.INVENTORY]: [
        { to: GAME_STATES.EXPLORATION, condition: () => false } // 实际游戏中应检查物品栏是否已关闭
      ],
      [GAME_STATES.DEATH]: [
        { to: GAME_STATES.EXPLORATION, condition: () => false } // 实际游戏中应检查玩家是否选择复活
      ],
      [GAME_STATES.IDLE]: [
        { to: GAME_STATES.EXPLORATION, condition: () => false }, // 实际游戏中应检查玩家是否有移动或其他操作
        { to: GAME_STATES.MENU, condition: () => false } // 实际游戏中应检查玩家是否按下菜单键
      ]
    };
  }

  /**
   * 初始化状态行为
   */
  private initStateActions(): void {
    this.stateActions = {
      [GAME_STATES.EXPLORATION]: {
        enter: () => console.log('进入探索状态：玩家可以自由移动、探索世界、与环境互动'),
        update: () => console.log('更新探索状态：处理玩家移动、环境变化、怪物AI等'),
        exit: () => console.log('退出探索状态')
      },
      [GAME_STATES.COMBAT]: {
        enter: () => console.log('进入战斗状态：玩家与敌人战斗，显示战斗UI'),
        update: () => console.log('更新战斗状态：处理攻击判定、伤害计算、技能效果等'),
        exit: () => console.log('退出战斗状态：隐藏战斗UI')
      },
      [GAME_STATES.DIALOGUE]: {
        enter: () => console.log('进入对话状态：显示对话界面，处理NPC对话'),
        update: () => console.log('更新对话状态：处理对话选项、对话进度等'),
        exit: () => console.log('退出对话状态：隐藏对话界面')
      },
      [GAME_STATES.TRADING]: {
        enter: () => console.log('进入交易状态：显示交易界面，允许玩家与NPC或其他玩家交易'),
        update: () => console.log('更新交易状态：处理物品交换、金币交易等'),
        exit: () => console.log('退出交易状态：隐藏交易界面')
      },
      [GAME_STATES.MENU]: {
        enter: () => console.log('进入菜单状态：显示游戏主菜单'),
        update: () => console.log('更新菜单状态：处理菜单选项、设置调整等'),
        exit: () => console.log('退出菜单状态：隐藏游戏主菜单')
      },
      [GAME_STATES.CHARACTER_PANEL]: {
        enter: () => console.log('进入角色面板状态：显示角色属性、技能等信息'),
        update: () => console.log('更新角色面板状态：处理属性点分配、技能学习等'),
        exit: () => console.log('退出角色面板状态：隐藏角色面板')
      },
      [GAME_STATES.INVENTORY]: {
        enter: () => console.log('进入物品栏状态：显示玩家物品、装备等'),
        update: () => console.log('更新物品栏状态：处理物品整理、装备更换等'),
        exit: () => console.log('退出物品栏状态：隐藏物品栏')
      },
      [GAME_STATES.DEATH]: {
        enter: () => console.log('进入死亡状态：显示死亡界面，玩家失去控制'),
        update: () => console.log('更新死亡状态：处理死亡计时、复活选项等'),
        exit: () => console.log('退出死亡状态：玩家复活，恢复控制')
      },
      [GAME_STATES.IDLE]: {
        enter: () => console.log('进入闲置状态：玩家无操作'),
        update: () => console.log('更新闲置状态：处理闲置时间、自动回复等'),
        exit: () => console.log('退出闲置状态')
      }
    };
  }

  /**
   * 获取指定状态的转换条件
   * @param state 当前状态
   * @returns 转换条件数组
   */
  public getTransitions(state: GameState): TransitionMap[keyof TransitionMap] {
    return this.transitions[state] || [];
  }

  /**
   * 获取指定状态的行为
   * @param state 当前状态
   * @returns 状态行为对象
   */
  public getStateActions(state: GameState): StateActionMap[keyof StateActionMap] | undefined {
    return this.stateActions[state];
  }

  /**
   * 执行状态进入行为
   * @param state 要进入的状态
   */
  public onEnterState(state: GameState): void {
    const actions = this.getStateActions(state);
    if (actions?.enter) {
      actions.enter();
    }
  }

  /**
   * 执行状态退出行为
   * @param state 要退出的状态
   */
  public onExitState(state: GameState): void {
    const actions = this.getStateActions(state);
    if (actions?.exit) {
      actions.exit();
    }
  }

  /**
   * 执行状态更新行为
   * @param state 要更新的状态
   */
  public onUpdateState(state: GameState): void {
    const actions = this.getStateActions(state);
    if (actions?.update) {
      actions.update();
    }
  }
}